home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / MAME / src / sound / tms36xx.c < prev    next >
C/C++ Source or Header  |  2000-04-23  |  16KB  |  566 lines

  1. #include "driver.h"
  2.  
  3. #define VERBOSE 1
  4.  
  5. #if VERBOSE
  6. #define LOG(x) logerror x
  7. #else
  8. #define LOG(x)
  9. #endif
  10.  
  11. #define VMIN    0x0000
  12. #define VMAX    0x7fff
  13.  
  14. /* the frequencies are later adjusted by "* clock / FSCALE" */
  15. #define FSCALE    1024
  16.  
  17. struct TMS36XX {
  18.     char *subtype;        /* subtype name MM6221AA, TMS3615 or TMS3617 */
  19.     int channel;        /* returned by stream_init() */
  20.  
  21.     int samplerate;     /* from Machine->sample_rate */
  22.  
  23.     int basefreq;        /* chip's base frequency */
  24.     int octave;         /* octave select of the TMS3615 */
  25.  
  26.     int speed;            /* speed of the tune */
  27.     int tune_counter;    /* tune counter */
  28.     int note_counter;    /* note counter */
  29.  
  30.     int voices;         /* active voices */
  31.     int shift;            /* shift toggles between 0 and 6 to allow decaying voices */
  32.     int vol[12];        /* (decaying) volume of harmonics notes */
  33.     int vol_counter[12];/* volume adjustment counter */
  34.     int decay[12];        /* volume adjustment rate - dervied from decay */
  35.  
  36.     int counter[12];    /* tone frequency counter */
  37.     int frequency[12];    /* tone frequency */
  38.     int output;         /* output signal bits */
  39.     int enable;         /* mask which harmoics */
  40.  
  41.     int tune_num;        /* tune currently playing */
  42.     int tune_ofs;        /* note currently playing */
  43.     int tune_max;        /* end of tune */
  44. };
  45.  
  46. static struct TMS36XXinterface *intf;
  47. static struct TMS36XX *tms36xx[MAX_TMS36XX];
  48.  
  49. #define C(n)    (int)((FSCALE<<(n-1))*1.18921)    /* 2^(3/12) */
  50. #define Cx(n)    (int)((FSCALE<<(n-1))*1.25992)    /* 2^(4/12) */
  51. #define D(n)    (int)((FSCALE<<(n-1))*1.33484)    /* 2^(5/12) */
  52. #define Dx(n)    (int)((FSCALE<<(n-1))*1.41421)    /* 2^(6/12) */
  53. #define E(n)    (int)((FSCALE<<(n-1))*1.49831)    /* 2^(7/12) */
  54. #define F(n)    (int)((FSCALE<<(n-1))*1.58740)    /* 2^(8/12) */
  55. #define Fx(n)    (int)((FSCALE<<(n-1))*1.68179)    /* 2^(9/12) */
  56. #define G(n)    (int)((FSCALE<<(n-1))*1.78180)    /* 2^(10/12) */
  57. #define Gx(n)    (int)((FSCALE<<(n-1))*1.88775)    /* 2^(11/12) */
  58. #define A(n)    (int)((FSCALE<<n))                /* A */
  59. #define Ax(n)    (int)((FSCALE<<n)*1.05946)        /* 2^(1/12) */
  60. #define B(n)    (int)((FSCALE<<n)*1.12246)        /* 2^(2/12) */
  61.  
  62. /*
  63.  * Alarm sound?
  64.  * It is unknown what this sound is like. Until somebody manages
  65.  * trigger sound #1 of the Phoenix PCB sound chip I put just something
  66.  * 'alarming' in here.
  67.  */
  68. static int tune1[96*6] = {
  69.     C(3),    0,        0,        C(2),    0,        0,
  70.     G(3),    0,        0,        0,        0,        0,
  71.     C(3),    0,        0,        0,        0,        0,
  72.     G(3),    0,        0,        0,        0,        0,
  73.     C(3),    0,        0,        0,        0,        0,
  74.     G(3),    0,        0,        0,        0,        0,
  75.     C(3),    0,        0,        0,        0,        0,
  76.     G(3),    0,        0,        0,        0,        0,
  77.     C(3),    0,        0,        C(4),    0,        0,
  78.     G(3),    0,        0,        0,        0,        0,
  79.     C(3),    0,        0,        0,        0,        0,
  80.     G(3),    0,        0,        0,        0,        0,
  81.     C(3),    0,        0,        0,        0,        0,
  82.     G(3),    0,        0,        0,        0,        0,
  83.     C(3),    0,        0,        0,        0,        0,
  84.     G(3),    0,        0,        0,        0,        0,
  85.     C(3),    0,        0,        C(2),    0,        0,
  86.     G(3),    0,        0,        0,        0,        0,
  87.     C(3),    0,        0,        0,        0,        0,
  88.     G(3),    0,        0,        0,        0,        0,
  89.     C(3),    0,        0,        0,        0,        0,
  90.     G(3),    0,        0,        0,        0,        0,
  91.     C(3),    0,        0,        0,        0,        0,
  92.     G(3),    0,        0,        0,        0,        0,
  93.     C(3),    0,        0,        C(4),    0,        0,
  94.     G(3),    0,        0,        0,        0,        0,
  95.     C(3),    0,        0,        0,        0,        0,
  96.     G(3),    0,        0,        0,        0,        0,
  97.     C(3),    0,        0,        0,        0,        0,
  98.     G(3),    0,        0,        0,        0,        0,
  99.     C(3),    0,        0,        0,        0,        0,
  100.     G(3),    0,        0,        0,        0,        0,
  101. };
  102.  
  103. /*
  104.  * Fuer Elise, Beethoven
  105.  * (Excuse my non-existent musical skill, Mr. B ;-)
  106.  */
  107. static int tune2[96*6] = {
  108.     D(3),    D(4),    D(5),    0,        0,        0,
  109.     Cx(3),    Cx(4),    Cx(5),    0,        0,        0,
  110.     D(3),    D(4),    D(5),    0,        0,        0,
  111.     Cx(3),    Cx(4),    Cx(5),    0,        0,        0,
  112.     D(3),    D(4),    D(5),    0,        0,        0,
  113.     A(2),    A(3),    A(4),    0,        0,        0,
  114.     C(3),    C(4),    C(5),    0,        0,        0,
  115.     Ax(2),    Ax(3),    Ax(4),    0,        0,        0,
  116.     G(2),    G(3),    G(4),    0,        0,        0,
  117.     D(1),    D(2),    D(3),    0,        0,        0,
  118.     G(1),    G(2),    G(3),    0,        0,        0,
  119.     Ax(1),    Ax(2),    Ax(3),    0,        0,        0,
  120.  
  121.     D(2),    D(3),    D(4),    0,        0,        0,
  122.     G(2),    G(3),    G(4),    0,        0,        0,
  123.     A(2),    A(3),    A(4),    0,        0,        0,
  124.     D(1),    D(2),    D(3),    0,        0,        0,
  125.     A(1),    A(2),    A(3),    0,        0,        0,
  126.     D(2),    D(3),    D(4),    0,        0,        0,
  127.     Fx(2),    Fx(3),    Fx(4),    0,        0,        0,
  128.     A(2),    A(3),    A(4),    0,        0,        0,
  129.     Ax(2),    Ax(3),    Ax(4),    0,        0,        0,
  130.     D(1),    D(2),    D(3),    0,        0,        0,
  131.     G(1),    G(2),    G(3),    0,        0,        0,
  132.     Ax(1),    Ax(2),    Ax(3),    0,        0,        0,
  133.  
  134.     D(3),    D(4),    D(5),    0,        0,        0,
  135.     Cx(3),    Cx(4),    Cx(5),    0,        0,        0,
  136.     D(3),    D(4),    D(5),    0,        0,        0,
  137.     Cx(3),    Cx(4),    Cx(5),    0,        0,        0,
  138.     D(3),    D(4),    D(5),    0,        0,        0,
  139.     A(2),    A(3),    A(4),    0,        0,        0,
  140.     C(3),    C(4),    C(5),    0,        0,        0,
  141.     Ax(2),    Ax(3),    Ax(4),    0,        0,        0,
  142.     G(2),    G(3),    G(4),    0,        0,        0,
  143.     D(1),    D(2),    D(3),    0,        0,        0,
  144.     G(1),    G(2),    G(3),    0,        0,        0,
  145.     Ax(1),    Ax(2),    Ax(3),    0,        0,        0,
  146.  
  147.     D(2),    D(3),    D(4),    0,        0,        0,
  148.     G(2),    G(3),    G(4),    0,        0,        0,
  149.     A(2),    A(3),    A(4),    0,        0,        0,
  150.     D(1),    D(2),    D(3),    0,        0,        0,
  151.     A(1),    A(2),    A(3),    0,        0,        0,
  152.     D(2),    D(3),    D(4),    0,        0,        0,
  153.     Ax(2),    Ax(3),    Ax(4),    0,        0,        0,
  154.     A(2),    A(3),    A(4),    0,        0,        0,
  155.     0,        0,        0,        G(2),    G(3),    G(4),
  156.     D(1),    D(2),    D(3),    0,        0,        0,
  157.     G(1),    G(2),    G(3),    0,        0,        0,
  158.     0,        0,        0,        0,        0,        0
  159. };
  160.  
  161. /*
  162.  * The theme from Phoenix, a sad little tune.
  163.  * Gerald Coy:
  164.  *     The starting song from Phoenix is coming from a old french movie and
  165.  *     it's called : "Jeux interdits" which means "unallowed games"  ;-)
  166.  * Mirko Buffoni:
  167.  *     It's called "Sogni proibiti" in italian, by Anonymous.
  168.  * Magic*:
  169.  *     This song is a classical piece called "ESTUDIO" from M.A.Robira.
  170.  */
  171. static int tune3[96*6] = {
  172.     A(2),    A(3),    A(4),    D(1),     D(2),      D(3),
  173.     0,        0,        0,        0,         0,       0,
  174.     A(2),    A(3),    A(4),    0,         0,       0,
  175.     0,        0,        0,        0,         0,       0,
  176.     A(2),    A(3),    A(4),    0,         0,       0,
  177.     0,        0,        0,        0,         0,       0,
  178.  
  179.     A(2),    A(3),    A(4),    A(1),     A(2),      A(3),
  180.     0,        0,        0,        0,         0,       0,
  181.     G(2),    G(3),    G(4),    0,         0,       0,
  182.     0,        0,        0,        0,         0,       0,
  183.     F(2),    F(3),    F(4),    0,         0,       0,
  184.     0,        0,        0,        0,         0,       0,
  185.  
  186.     F(2),    F(3),    F(4),    F(1),     F(2),      F(3),
  187.     0,        0,        0,        0,         0,       0,
  188.     E(2),    E(3),    E(4),    F(1),     F(2),      F(3),
  189.     0,        0,        0,        0,         0,       0,
  190.     D(2),    D(3),    D(4),    F(1),     F(2),      F(3),
  191.     0,        0,        0,        0,         0,       0,
  192.  
  193.     D(2),    D(3),    D(4),    A(1),     A(2),      A(3),
  194.     0,        0,        0,        0,         0,       0,
  195.     F(2),    F(3),    F(4),    0,         0,       0,
  196.     0,        0,        0,        0,         0,       0,
  197.     A(2),    A(3),    A(4),    0,         0,       0,
  198.     0,        0,        0,        0,         0,       0,
  199.  
  200.     D(3),    D(4),    D(5),    D(1),     D(2),      D(3),
  201.     0,        0,        0,        0,         0,       0,
  202.     0,        0,        0,        D(1),     D(2),      D(3),
  203.     0,        0,        0,        F(1),     F(2),      F(3),
  204.     0,        0,        0,        A(1),     A(2),      A(3),
  205.     0,        0,        0,        D(2),     D(2),      D(2),
  206.  
  207.     D(3),    D(4),    D(5),    D(1),     D(2),      D(3),
  208.     0,        0,        0,        0,         0,       0,
  209.     C(3),    C(4),    C(5),    0,         0,       0,
  210.     0,        0,        0,        0,         0,       0,
  211.     Ax(2),    Ax(3),    Ax(4),    0,         0,       0,
  212.     0,        0,        0,        0,         0,       0,
  213.  
  214.     Ax(2),    Ax(3),    Ax(4),    Ax(1),     Ax(2),   Ax(3),
  215.     0,        0,        0,        0,         0,       0,
  216.     A(2),    A(3),    A(4),    0,         0,       0,
  217.     0,        0,        0,        0,         0,       0,
  218.     G(2),    G(3),    G(4),    0,         0,       0,
  219.     0,        0,        0,        0,         0,       0,
  220.  
  221.     G(2),    G(3),    G(4),    G(1),     G(2),      G(3),
  222.     0,        0,        0,        0,         0,       0,
  223.     A(2),    A(3),    A(4),    0,         0,       0,
  224.     0,        0,        0,        0,         0,       0,
  225.     Ax(2),    Ax(3),    Ax(4),    0,         0,       0,
  226.     0,        0,        0,        0,         0,       0,
  227.  
  228.     A(2),    A(3),    A(4),    A(1),     A(2),      A(3),
  229.     0,        0,        0,        0,         0,       0,
  230.     Ax(2),    Ax(3),    Ax(4),    0,         0,       0,
  231.     0,        0,        0,        0,         0,       0,
  232.     A(2),    A(3),    A(4),    0,         0,       0,
  233.     0,        0,        0,        0,         0,       0,
  234.  
  235.     Cx(3),    Cx(4),    Cx(5),    A(1),     A(2),      A(3),
  236.     0,        0,        0,        0,         0,       0,
  237.     Ax(2),    Ax(3),    Ax(4),    0,         0,       0,
  238.     0,        0,        0,        0,         0,       0,
  239.     A(2),    A(3),    A(4),    0,         0,       0,
  240.     0,        0,        0,        0,         0,       0,
  241.  
  242.     A(2),    A(3),    A(4),    F(1),     F(2),      F(3),
  243.     0,        0,        0,        0,         0,       0,
  244.     G(2),    G(3),    G(4),    0,         0,       0,
  245.     0,        0,        0,        0,         0,       0,
  246.     F(2),    F(3),    F(4),    0,         0,       0,
  247.     0,        0,        0,        0,         0,       0,
  248.  
  249.     F(2),    F(3),    F(4),    D(1),     D(2),      D(3),
  250.     0,        0,        0,        0,         0,       0,
  251.     E(2),    E(3),    E(4),    0,         0,       0,
  252.     0,        0,        0,        0,         0,       0,
  253.     D(2),    D(3),    D(4),    0,         0,       0,
  254.     0,        0,        0,        0,         0,       0,
  255.  
  256.     E(2),    E(3),    E(4),    E(1),     E(2),      E(3),
  257.     0,        0,        0,        0,         0,       0,
  258.     E(2),    E(3),    E(4),    0,         0,       0,
  259.     0,        0,        0,        0,         0,       0,
  260.     E(2),    E(3),    E(4),    0,         0,       0,
  261.     0,        0,        0,        0,         0,       0,
  262.  
  263.     E(2),    E(3),    E(4),    Ax(1),     Ax(2),   Ax(3),
  264.     0,        0,        0,        0,         0,       0,
  265.     F(2),    F(3),    F(4),    0,         0,       0,
  266.     0,        0,        0,        0,         0,       0,
  267.     E(2),    E(3),    E(4),    F(1),     F(2),      F(3),
  268.     0,        0,        0,        0,         0,       0,
  269.  
  270.     D(2),    D(3),    D(4),    D(1),     D(2),      D(3),
  271.     0,        0,        0,        0,         0,       0,
  272.     F(2),    F(3),    F(4),    A(1),     A(2),      A(3),
  273.     0,        0,        0,        0,         0,       0,
  274.     A(2),    A(3),    A(4),    F(1),     F(2),      F(3),
  275.     0,        0,        0,        0,         0,       0,
  276.  
  277.     D(3),    D(4),    D(5),    D(1),     D(2),      D(3),
  278.     0,        0,        0,        0,         0,       0,
  279.     0,        0,        0,        0,         0,       0,
  280.     0,        0,        0,        0,         0,       0,
  281.     0,        0,        0,        0,         0,       0,
  282.     0,        0,        0,        0,         0,       0
  283. };
  284.  
  285. /* This is used to play single notes for the TMS3615/TMS3617 */
  286. static int tune4[13*6] = {
  287. /*    16'     8'      5 1/3'  4'      2 2/3'  2'      */
  288.     B(0),    B(1),    Dx(2),    B(2),    Dx(3),    B(3),
  289.     C(1),    C(2),    E(2),    C(3),    E(3),    C(4),
  290.     Cx(1),    Cx(2),    F(2),    Cx(3),    F(3),    Cx(4),
  291.     D(1),    D(2),    Fx(2),    D(3),    Fx(3),    D(4),
  292.     Dx(1),    Dx(2),    G(2),    Dx(3),    G(3),    Dx(4),
  293.     E(1),    E(2),    Gx(2),    E(3),    Gx(3),    E(4),
  294.     F(1),    F(2),    A(2),    F(3),    A(3),    F(4),
  295.     Fx(1),    Fx(2),    Ax(2),    Fx(3),    Ax(3),    Fx(4),
  296.     G(1),    G(2),    B(2),    G(3),    B(3),    G(4),
  297.     Gx(1),    Gx(2),    C(3),    Gx(3),    C(4),    Gx(4),
  298.     A(1),    A(2),    Cx(3),    A(3),    Cx(4),    A(4),
  299.     Ax(1),    Ax(2),    D(3),    Ax(3),    D(4),    Ax(4),
  300.     B(1),    B(2),    Dx(3),    B(3),    Dx(4),    B(4)
  301. };
  302.  
  303. static int *tunes[] = {NULL,tune1,tune2,tune3,tune4};
  304.  
  305. #define DECAY(voice)                                            \
  306.     if( tms->vol[voice] > VMIN )                                \
  307.     {                                                            \
  308.         /* decay of first voice */                                \
  309.         tms->vol_counter[voice] -= tms->decay[voice];            \
  310.         while( tms->vol_counter[voice] <= 0 )                    \
  311.         {                                                        \
  312.             tms->vol_counter[voice] += samplerate;                \
  313.             if( tms->vol[voice]-- <= VMIN )                     \
  314.             {                                                    \
  315.                 tms->frequency[voice] = 0;                        \
  316.                 tms->vol[voice] = VMIN;                         \
  317.                 break;                                            \
  318.             }                                                    \
  319.         }                                                        \
  320.     }
  321.  
  322. #define RESTART(voice)                                            \
  323.     if( tunes[tms->tune_num][tms->tune_ofs*6+voice] )            \
  324.     {                                                            \
  325.         tms->frequency[tms->shift+voice] =                        \
  326.             tunes[tms->tune_num][tms->tune_ofs*6+voice] *        \
  327.             (tms->basefreq << tms->octave) / FSCALE;            \
  328.         tms->vol[tms->shift+voice] = VMAX;                        \
  329.     }
  330.  
  331. #define TONE(voice)                                             \
  332.     if( (tms->enable & (1<<voice)) && tms->frequency[voice] )    \
  333.     {                                                            \
  334.         /* first note */                                        \
  335.         tms->counter[voice] -= tms->frequency[voice];            \
  336.         while( tms->counter[voice] <= 0 )                        \
  337.         {                                                        \
  338.             tms->counter[voice] += samplerate;                    \
  339.             tms->output ^= 1 << voice;                            \
  340.         }                                                        \
  341.         if (tms->output & tms->enable & (1 << voice))            \
  342.             sum += tms->vol[voice];                             \
  343.     }
  344.  
  345.  
  346.  
  347. static void tms36xx_sound_update(int param, INT16 *buffer, int length)
  348. {
  349.     struct TMS36XX *tms = tms36xx[param];
  350.     int samplerate = tms->samplerate;
  351.  
  352.     /* no tune played? */
  353.     if( !tunes[tms->tune_num] || tms->voices == 0 )
  354.     {
  355.         while (--length >= 0)
  356.             buffer[length] = 0;
  357.         return;
  358.     }
  359.  
  360.     while( length-- > 0 )
  361.     {
  362.         int sum = 0;
  363.  
  364.         /* decay the twelve voices */
  365.         DECAY( 0) DECAY( 1) DECAY( 2) DECAY( 3) DECAY( 4) DECAY( 5)
  366.         DECAY( 6) DECAY( 7) DECAY( 8) DECAY( 9) DECAY(10) DECAY(11)
  367.  
  368.         /* musical note timing */
  369.         tms->tune_counter -= tms->speed;
  370.         if( tms->tune_counter <= 0 )
  371.         {
  372.             int n = (-tms->tune_counter / samplerate) + 1;
  373.             tms->tune_counter += n * samplerate;
  374.  
  375.             if( (tms->note_counter -= n) <= 0 )
  376.             {
  377.                 tms->note_counter += VMAX;
  378.                 if (tms->tune_ofs < tms->tune_max)
  379.                 {
  380.                     /* shift to the other 'bank' of voices */
  381.                     tms->shift ^= 6;
  382.                     /* restart one 'bank' of voices */
  383.                     RESTART(0) RESTART(1) RESTART(2)
  384.                     RESTART(3) RESTART(4) RESTART(5)
  385.                     tms->tune_ofs++;
  386.                 }
  387.             }
  388.         }
  389.  
  390.         /* update the twelve voices */
  391.         TONE( 0) TONE( 1) TONE( 2) TONE( 3) TONE( 4) TONE( 5)
  392.         TONE( 6) TONE( 7) TONE( 8) TONE( 9) TONE(10) TONE(11)
  393.  
  394.         *buffer++ = sum / tms->voices;
  395.     }
  396. }
  397.  
  398. static void tms36xx_reset_counters(int chip)
  399. {
  400.     struct TMS36XX *tms = tms36xx[chip];
  401.     tms->tune_counter = 0;
  402.     tms->note_counter = 0;
  403.     memset(tms->vol_counter, 0, sizeof(tms->vol_counter));
  404.     memset(tms->counter, 0, sizeof(tms->counter));
  405. }
  406.  
  407. void mm6221aa_tune_w(int chip, int tune)
  408. {
  409.     struct TMS36XX *tms = tms36xx[chip];
  410.  
  411.     /* which tune? */
  412.     tune &= 3;
  413.     if( tune == tms->tune_num )
  414.         return;
  415.  
  416.     LOG(("%s tune:%X\n", tms->subtype, tune));
  417.  
  418.     /* update the stream before changing the tune */
  419.     stream_update(tms->channel,0);
  420.  
  421.     tms->tune_num = tune;
  422.     tms->tune_ofs = 0;
  423.     tms->tune_max = 96; /* fixed for now */
  424. }
  425.  
  426. void tms36xx_note_w(int chip, int octave, int note)
  427. {
  428.     struct TMS36XX *tms = tms36xx[chip];
  429.  
  430.     octave &= 3;
  431.     note &= 15;
  432.  
  433.     if (note > 12)
  434.         return;
  435.  
  436.     LOG(("%s octave:%X note:%X\n", tms->subtype, octave, note));
  437.  
  438.     /* update the stream before changing the tune */
  439.     stream_update(tms->channel,0);
  440.  
  441.     /* play a single note from 'tune 4', a list of the 13 tones */
  442.     tms36xx_reset_counters(chip);
  443.     tms->octave = octave;
  444.     tms->tune_num = 4;
  445.     tms->tune_ofs = note;
  446.     tms->tune_max = note + 1;
  447. }
  448.  
  449. void tms3617_enable_w(int chip, int enable)
  450. {
  451.     struct TMS36XX *tms = tms36xx[chip];
  452.     int i, bits = 0;
  453.  
  454.     /* duplicate the 6 voice enable bits */
  455.     enable = (enable & 0x3f) | ((enable & 0x3f) << 6);
  456.     if (enable == tms->enable)
  457.         return;
  458.  
  459.     /* update the stream before changing the tune */
  460.     stream_update(tms->channel,0);
  461.  
  462.     LOG(("%s enable voices", tms->subtype));
  463.     for (i = 0; i < 6; i++)
  464.     {
  465.         if (enable & (1 << i))
  466.         {
  467.             bits += 2;    /* each voice has two instances */
  468. #if VERBOSE
  469.             switch (i)
  470.             {
  471.             case 0: LOG((" 16'")); break;
  472.             case 1: LOG((" 8'")); break;
  473.             case 2: LOG((" 5 1/3'")); break;
  474.             case 3: LOG((" 4'")); break;
  475.             case 4: LOG((" 2 2/3'")); break;
  476.             case 5: LOG((" 2'")); break;
  477.             }
  478. #endif
  479.         }
  480.     }
  481.     /* set the enable mask and number of active voices */
  482.     tms->enable = enable;
  483.     tms->voices = bits;
  484.     LOG(("%s\n", bits ? "" : " none"));
  485. }
  486.  
  487. int tms36xx_sh_start(const struct MachineSound *msound)
  488. {
  489.     int i, j;
  490.     intf = msound->sound_interface;
  491.  
  492.     for( i = 0; i < intf->num; i++ )
  493.     {
  494.         int enable;
  495.         struct TMS36XX *tms;
  496.         char name[16];
  497.  
  498.         if (intf->subtype[i] == MM6221AA)
  499.             sprintf(name, "MM6221AA #%d", i);
  500.         else
  501.             sprintf(name, "TMS36%02d #%d", intf->subtype[i], i);
  502.         tms36xx[i] = malloc(sizeof(struct TMS36XX));
  503.         if( !tms36xx[i] )
  504.         {
  505.             logerror("%s failed to malloc struct TMS36XX\n", name);
  506.             return 1;
  507.         }
  508.         tms = tms36xx[i];
  509.         memset(tms, 0, sizeof(struct TMS36XX));
  510.  
  511.         tms->subtype = malloc(strlen(name) + 1);
  512.         strcpy(tms->subtype, name);
  513.         tms->channel = stream_init(name, intf->mixing_level[i], Machine->sample_rate, i, tms36xx_sound_update);
  514.  
  515.         if( tms->channel == -1 )
  516.         {
  517.             logerror("%s stream_init failed\n", name);
  518.             return 1;
  519.         }
  520.         tms->samplerate = Machine->sample_rate ? Machine->sample_rate : 1;
  521.         tms->basefreq = intf->basefreq[i];
  522.         enable = 0;
  523.         for (j = 0; j < 6; j++)
  524.         {
  525.             if( intf->decay[i][j] > 0 )
  526.             {
  527.                 tms->decay[j+0] = tms->decay[j+6] = VMAX / intf->decay[i][j];
  528.                 enable |= 0x41 << j;
  529.             }
  530.         }
  531.         tms->speed = (intf->speed[i] > 0) ? VMAX / intf->speed[i] : VMAX;
  532.         tms3617_enable_w(i,enable);
  533.  
  534.         LOG(("%s samplerate    %d\n", name, tms->samplerate));
  535.         LOG(("%s basefreq      %d\n", name, tms->basefreq));
  536.         LOG(("%s decay         %d,%d,%d,%d,%d,%d\n", name,
  537.             tms->decay[0], tms->decay[1], tms->decay[2],
  538.             tms->decay[3], tms->decay[4], tms->decay[5]));
  539.         LOG(("%s speed         %d\n", name, tms->speed));
  540.     }
  541.     return 0;
  542. }
  543.  
  544. void tms36xx_sh_stop(void)
  545. {
  546.     int i;
  547.     for( i = 0; i < intf->num; i++ )
  548.     {
  549.         if( tms36xx[i] )
  550.         {
  551.             if (tms36xx[i]->subtype)
  552.                 free(tms36xx[i]->subtype);
  553.             free(tms36xx[i]);
  554.         }
  555.         tms36xx[i] = NULL;
  556.     }
  557. }
  558.  
  559. void tms36xx_sh_update(void)
  560. {
  561.     int i;
  562.     for( i = 0; i < intf->num; i++ )
  563.         stream_update(i,0);
  564. }
  565.  
  566.